var network = require("Network")
var power_key =  "earth_travel_power";
var power_upd_key =  "earth_travel_power_update_time";

var level_key =  "earth_travel_level";
var score_key =  "earth_travel_score";
var weight_key =  "earth_travel_weight";
var player_key =  "earth_travel_player";
var gold_key =  "earth_travel_gold";
var skill_key =  "earth_travel_skill";
var audio_switch_key = "earth_travel_audio_switch";
var audio_volume_key = "earth_travel_audio_volume";
var skill_introduce_key = "earth_travel_skill_introduce";
var user_key = "earth_travel_user_key";
module.exports = {
    /**
     * 默认声音配置
     */
    audio:{'effect': 1, 'music': 1},
    /**
     * 默认开关配置
     */
    audioSwitch:{'switchMusic': true, 'switchEffect': true},
    playSkinId:"0",
    /**
     * 默认技能配置
     */
    playSkill:[0,1,2,3,4],
    playSkillLevel:[1,1,1,1,1],
    /**
     * 默认金币配置
     */
    gold:0,
    /**
     * 默认体力值
     */
    powerDefault:20,
    /**
     * 默认player
     */
    playerDefault:0.8,

    /**
     * 体力值增加频繁（单位：秒）
     */
    powerIncreateFrequency:30,

    /**
     * 体力值单局消耗
     */
    powerCutOnce:1,

    /**
     * 最大体力值
     */
    maxPower:20,
    loginFlag:false,
    tips:["升级技能可以轻松过关！",
        "每10关难度提升1个档次。",
        "通关100关，制作人请你吃饭。",
        //"少壮不努力，老大做游戏。",
        "官方玩家群【894997438】",
        "优先升级第五个技能，神技！"],
    gameId:"1",
    newUser(){
        network.fetch({
            url:"/game/newUser",
            type:"POST",
            data:{gameId:this.gameId},
            success:function(result){
                if(result.code == 200){
                    cc.sys.localStorage.setItem(user_key,result.data);
                }else{
                    cc.log("网络请求异常")
                }
                cc.log("请求状态码："+result.code)
            }
        })
    },
    gameLogin(){
        var userId = cc.sys.localStorage.getItem(user_key)
        if(!userId){
            this.newUser();
        }
        if(userId && !this.loginFlag){
            var self = this;
            var level = this.getCurrentLevel();
            var gold = this.getGold();
            network.fetch({
                url:"/game/gameLog",
                type:"post",
                data:{gameId:self.gameId,userId:userId,level:level,gold:gold},
                success:function(data){
                    if(data.code==200){
                        self.loginFlag = true;
                    }
                    cc.log("请求状态码："+data.code)
                }
            })
        }


    },
    /**
     * 获取声音配置
     * add by lidu at 2019/6/20
     */
    getAudioSetting(){
        var audio = cc.sys.localStorage.getItem(audio_volume_key)
        if(audio){
            try{
                return JSON.parse(audio);
            }catch (e) {
                console.log(e)
            }
        }
        cc.sys.localStorage.setItem(audio_volume_key, JSON.stringify(this.audio));
        return this.audio;
    },
    saveAudioVolume(obj){
        cc.sys.localStorage.setItem(audio_volume_key,JSON.stringify(obj))
    },
    saveSwitchEffect(obj){
        cc.sys.localStorage.setItem(audio_switch_key,JSON.stringify(obj))
    },
    getSwitchSetting(){

        var audioSwitch = cc.sys.localStorage.getItem(audio_switch_key)
        if(audioSwitch){
            try{
                return JSON.parse(audioSwitch);
            }catch (e) {
                console.log(e)
            }
        }
        cc.sys.localStorage.setItem(audio_switch_key, JSON.stringify(this.audioSwitch));
        return this.audioSwitch;



    },
    /**
     * 查询当前所在关卡
     * add by lidu at 19/06/10
     */
    getCurrentLevel(){
        var level = cc.sys.localStorage.getItem(level_key);
        if(level){
            try{
                return parseInt(level);
            }catch (e) {
                console.log(e)
            }
        }
        cc.sys.localStorage.setItem(level_key,"1");
        return 1;
    },
    /**
     * 查询当前得分
     * add by lidu at 19/06/10
     */
    getCurrentScore(){

        var score = cc.sys.localStorage.getItem(score_key);
        if(score){
            try {
                return parseInt(score);
            }catch (e) {
                console.log(e)
            }
        }
        cc.sys.localStorage.setItem(score_key,"0");
        return 0;
    },
    /**
     * 查询当前所剩体力值
     * add by lidu at 19/06/10
     */
    getCurrentPower(){
        var power = cc.sys.localStorage.getItem(power_key)
        if(power){
            try{
                return parseInt(power);
            }catch (e) {
                cc.sys.localStorage.setItem(power_key,this.powerDefault);
                return this.powerDefault;
            }
        }else{
            cc.sys.localStorage.setItem(power_key,this.powerDefault);
            return this.powerDefault;
        }
    },

    getInitData(){
        var data = {};
        data.level = this.getCurrentLevel();
        data.power = this.getCurrentPower();
        data.score = this.getCurrentScore();
        return data;
    },
    /**
     * 消耗体力值
     * add by lidu at 19/6/11
     * @returns 返回剩余的体力值
     */
    powerCut(){
        var power = this.getCurrentPower();
        if( power>=this.powerCutOnce){
            power = power-this.powerCutOnce;
            cc.sys.localStorage.setItem(power_key,power)
        }else{
            power = -1;
        }
        return power;
    },
    /**
     * 体力值增加
     */
    powerIncrease(){
        var power = this.getCurrentPower();
        if(power<this.maxPower){
            power = power+1;
            cc.sys.localStorage.setItem(power_key,power)
            cc.sys.localStorage.setItem(power_upd_key,new Date().getTime())
        }
        return power;
    },
    /**
     * 获取离线时增长的体力值
     * @returns {number}
     */
    getPowerIncreateOffLine(){
        var lastUpd = cc.sys.localStorage.getItem(power_upd_key);
        if(lastUpd && !this.powerCount){
            lastUpd = parseInt(lastUpd)
            var now = new Date().getTime();

            var diff = (now-lastUpd)/(this.powerIncreateFrequency * 1000);
            diff = parseInt(diff)
            if(diff>0){
                var power = this.getCurrentPower();
                if((power+diff)>this.maxPower){
                    diff = this.maxPower-power;
                }
                cc.sys.localStorage.setItem(power_upd_key,now)
                cc.sys.localStorage.setItem(power_key,power+diff)
                return diff;
            }
        }
        return 0;
    },
    getWeight(){

        var weight = cc.sys.localStorage.getItem(weight_key);
        if(weight){
            return parseInt(weight);
        }else{
            cc.sys.localStorage.setItem(weight_key,"1");
            return 1;
        }
    },
    getPlayer(){

        var player = cc.sys.localStorage.getItem(player_key);
        if(player){
            try {
                return parseFloat(player);
            }catch (e) {
                cc.sys.localStorage.setItem(player_key,""+this.playerDefault);
                return this.playerDefault;
            }
        }else{
            cc.sys.localStorage.setItem(player_key,""+this.playerDefault);
            return this.playerDefault;
        }
    },
    netLevel(){
        var level = this.getCurrentLevel()+1;
        cc.sys.localStorage.setItem(level_key,level);
        var userId = cc.sys.localStorage.getItem(user_key);
        var gold = cc.sys.localStorage.getItem(gold_key);
        if(userId){
            network.fetch({
                url:"/game/roundComplete",
                type:"post",
                data:{gameId:this.gameId,userId:userId,level:level,gold:gold},
                success:function(data){
                    if(data.code!=200){
                        cc.log("网点异常")
                    }
                }
            })
        }
    },
    getGold(){
        var gold = cc.sys.localStorage.getItem(gold_key);
        if(gold){
            try {
                return parseInt(gold);
            }catch (e) {
                cc.sys.localStorage.setItem(gold_key,""+this.gold);
                return this.gold;
            }
        }else{
            cc.sys.localStorage.setItem(gold_key,""+this.gold);
            return this.gold;
        }
    },

    addGold(newGold)
    {
        var gold = this.getGold()+newGold;
        cc.sys.localStorage.setItem(gold_key,gold);
        console.log("gold" +gold);

    },
    getHeroSkill(){
        var skill = cc.sys.localStorage.getItem(skill_key);
        if(skill){
            try {
                return JSON.parse(skill)
            }catch (e) {
                cc.sys.localStorage.setItem(skill_key,JSON.stringify(this.playSkillLevel))
                return this.playSkillLevel
            }
        }else{
            cc.sys.localStorage.setItem(skill_key,JSON.stringify(this.playSkillLevel))
            return this.playSkillLevel
        }
    },
    updateHeroSkill(skillId,skillValue){
        var skill = this.getHeroSkill();
        skill[skillId] = skillValue+1;
        cc.sys.localStorage.setItem(skill_key,JSON.stringify(skill))
    },
    /**
     * 获取当前待介绍的技能ID
     */
    getSkillIntroduceIndex(){
        if(this._skillIntroduceIndex == undefined){
            this._skillIntroduceIndex = 0;
            return 0

        }else if(this._skillIntroduceIndex<5){
            this._skillIntroduceIndex++;
            return this._skillIntroduceIndex;
        }else{
            this._skillIntroduceIndex = 1;
            return 1;
        }
    },
    getGoldLabel(gold){
        gold = gold||this.getGold();
        if(gold<1000){
            return gold+"B"
        }else if(gold<1000*1000){
            return (gold/1000).toFixed(1)+"KB"
        }else if(gold<1000*1000*1000){
            return (gold/(1000*1000)).toFixed(1)+"MB"
        }else if(gold<1000*1000*1000*1000){
            return (gold/(1000*1000*1000)).toFixed(1)+"GB"
        }else if(gold<1000*1000*1000*1000*1000){
            return (gold/(1000*1000*1000*1000)).toFixed(1)+"TB"
        }else if(gold<1000*1000*1000*1000*1000*1000){
            return (gold/(1000*1000*1000*1000*1000)).toFixed(1)+"PB"
        }else if(gold<1000*1000*1000*1000*1000*1000*1000){
            return (gold/(1000*1000*1000*1000*1000*1000)).toFixed(1)+"EB"
        }else if(gold<1000*1000*1000*1000*1000*1000*1000*1000){
            return (gold/(1000*1000*1000*1000*1000*1000*1000)).toFixed(1)+"ZB"
        }else if(gold<1000*1000*1000*1000*1000*1000*1000*1000*1000){
            return (gold/(1000*1000*1000*1000*1000*1000*1000*1000)).toFixed(1)+"YB"
        }else{
            return "无穷多"
        }
    }

}